"use strict";
cc._RF.push(module, '37a94RquNtPRrBYMb3cQVe6', 'HttpUtils');
// libs/lightMVC/util/HttpUtils.ts

"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property;
var HttpUtils = /** @class */ (function (_super) {
    __extends(HttpUtils, _super);
    function HttpUtils() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    /**
     * Get方法
     * @param {请求网址} url
     * @param {回调} callback
     * @param {头部参数} headParams
     */
    HttpUtils.Get = function (url, callback, headParams) {
        if (headParams === void 0) { headParams = null; }
        var xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function () {
            //如果请求状态为Done的话，则执行回调
            if (xhr.readyState == 4) {
                if (xhr.status >= 200 && xhr.status < 400) {
                    var response = xhr.responseText;
                    callback(null, response);
                }
                else {
                    callback("error", xhr.responseText);
                }
            }
        };
        xhr.open("GET", url, true);
        if (headParams) {
            for (var key in headParams) {
                xhr.setRequestHeader(key, headParams[key]);
            }
        }
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhr.send();
    };
    /**
     * Post请求，表单提交
     * @param url 请求的网址
     * @param reqData 请求的数据
     * @param callback 回调函数
     */
    HttpUtils.Post = function (url, reqData, callback, headParams) {
        if (headParams === void 0) { headParams = null; }
        //拼接请求参数
        var param = "";
        for (var item in reqData) {
            param += item + "=" + reqData[item] + "&";
        }
        //console.log(param);
        //发起请求
        var xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4) {
                if (xhr.status >= 200 && xhr.status < 400) {
                    var response = xhr.responseText;
                    callback(null, response);
                }
                else {
                    callback("error", xhr.responseText);
                }
            }
        };
        xhr.open("POST", url, true);
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        if (headParams) {
            for (var key in headParams) {
                xhr.setRequestHeader(key, headParams[key]);
            }
        }
        xhr.send(param); //reqData为字符串形式： "key=value"
    };
    HttpUtils = __decorate([
        ccclass
    ], HttpUtils);
    return HttpUtils;
}(cc.Component));
exports.default = HttpUtils;

cc._RF.pop();